package com.txsj.scan.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.txsj.scan.R;
import com.txsj.scan.bean.GoodsInfo;

import java.util.ArrayList;
import java.util.List;

public class AllocationAdapter extends RecyclerView.Adapter<AllocationAdapter.AllocationViewHolder> {
    private Context context;
    private List<GoodsInfo> list;

    public AllocationAdapter(Context context) {
        this.context = context;
        this.list = new ArrayList<>();
    }

    public void setData(List<GoodsInfo> list) {
        this.list.clear();
        this.list.addAll(list);
        notifyItemRangeChanged(this.list.size() - list.size(), list.size());
    }

    @Override
    public AllocationViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new AllocationViewHolder(LayoutInflater.from(context).inflate(R.layout.item_allocation, parent, false));
    }

    @Override
    public void onBindViewHolder(AllocationViewHolder holder, final int position) {
        if (null != list.get(position)) {
//            GlideImgManager.getLoadRoundImg(context, list.get(position).getGoods_img(), holder.iv_goods_pic);
            holder.tv_goods_name.setText(list.get(position).getGoods_name());
            holder.tv_goods_stockCount.setText("数量：" + list.get(position).getEpcList().size());
            holder.tv_goods_num.setText(list.get(position).getGoods_num());
            holder.iv_del.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (null != listener) listener.onDelete(position);
                }
            });
        }
    }

    public List<GoodsInfo> getList() {
        return list;
    }

    public void removeData(int position) {
        if (position < list.size()) {
            list.remove(position);
        }
        notifyDataSetChanged();
    }

    @Override
    public int getItemCount() {
        return list.size() > 0 ? list.size() : 0;
    }

    public class AllocationViewHolder extends RecyclerView.ViewHolder {

        ImageView iv_goods_pic, iv_del;
        TextView tv_goods_name, tv_goods_stockCount;
        TextView tv_goods_num; //条码

        public AllocationViewHolder(View itemView) {
            super(itemView);
//            iv_goods_pic = (ImageView) itemView.findViewById(R.id.iv_goods_pic);
            iv_del = (ImageView) itemView.findViewById(R.id.iv_del);
            tv_goods_name = (TextView) itemView.findViewById(R.id.tv_goods_name);
            tv_goods_stockCount = (TextView) itemView.findViewById(R.id.tv_goods_stockCount);
            tv_goods_num = (TextView) itemView.findViewById(R.id.tv_goods_num);
        }
    }

    private OnItemClickListener listener;

    public void setListener(OnItemClickListener listener) {
        this.listener = listener;
    }

    public interface OnItemClickListener {
//        void onItemClick(int pisition);

        void onDelete(int position);
    }
}
